//
//  XSStatusCell.swift
//  WeiDo
//
//  Created by XISHAN on 16/3/6.
//  Copyright © 2016年 XISHAN. All rights reserved.
//

import UIKit
import SnapKit


let XSStatusCellMargin: CGFloat = 8

let XSStatusCellHeadImageWH : CGFloat = 40

class XSStatusCell: UITableViewCell {

    var transmitBarTopConstraint: Constraint?
    
    var statusViewModel: XSStatusViewModel? {
        
        didSet{
            
            originalView.statusViewModel = statusViewModel
            transmitBarView.statusViewModel = statusViewModel
            
            // 干掉之前的约束
            // 1. 取到之前的约束 --> self.toolBarTopConstraint
            // 2. 怎么干掉呢?????
            self.transmitBarTopConstraint?.uninstall()
            
            // 判断是否有转发微博
            if statusViewModel?.status?.retweeted_status != nil {
                // 0. 给转发微博视图设置数据
                subContentView.statusViewModel = statusViewModel
                // 1. 显示 转发微博的视图
                subContentView.hidden = false
                // 2. toolBar的顶部等于转发微博的底部
                transmitBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.transmitBarTopConstraint = make.top.equalTo(subContentView.snp_bottom).constraint
                })
            }else{
                // 1. 隐藏 转发微博的视图
                subContentView.hidden = true
                // 2. toolBar的顶部等于原创微博的底部
                transmitBarView.snp_updateConstraints(closure: { (make) -> Void in
                    self.transmitBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }

            
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        selectionStyle = .None
        // 设置当前cell的背景颜色
        contentView.backgroundColor = UIColor(white: 239/255, alpha: 1)
        
        contentView.addSubview(originalView)
        contentView.addSubview(subContentView)
        contentView.addSubview(transmitBarView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(XSStatusCellMargin)

            //make.height.equalTo(200)
        }
        
        subContentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(originalView)
        }
        
        transmitBarView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(originalView)
            self.transmitBarTopConstraint = make.top.equalTo(subContentView.snp_bottom).constraint
            
            make.height.equalTo(35)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(transmitBarView)
        }
       
    }
    
    private lazy var originalView:XSOriginalView = XSOriginalView()
    
    private lazy var transmitBarView:XSTransmitBarView = XSTransmitBarView()
    
    private lazy var subContentView: XSSubContentView = XSSubContentView()
}
